<?php

namespace App\Shop\Controllers;

use App\Admin\Forms\Service\HandleConfirmOrder;
use App\Admin\Forms\Service\HandleRefund;
use App\Admin\Repositories\GoodOrder;
use App\Admin\Repositories\GoodOrderDetail;
use App\Shop\Forms\Service\HandleGoodOrder;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class GoodOrderController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        $row = new Row;
        $row->column(12,Card::make($this->order()));
        return $row;
    }

    protected function makeGrid(\Closure $callback): Grid
    {
        return Grid::make(new GoodOrder(['shop','user']), function (Grid $grid) use ($callback) {
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id);
            $this->common($grid);
            $callback($grid);
            $grid->column('state','订单状态')->using(\App\Models\GoodOrder::getStateList())->label(['default','primary','success','success','warning','danger']);
            $grid->column('action','操作')->display(function (){
                $url = url('shop/good-order')."/".$this->id."/edit";
                return "<a class='btn btn-info btn-sm' href='{$url}' >详情</a>";
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order_no');
                $filter->equal('pay_way')->select(['余额支付','微信支付']);
                $filter->equal('method')->select(['快递配送','到店自提']);
                $filter->like('address->name','收件人名称');
                $filter->like('address->phone','收件人电话');
                $filter->like('address->address','收件地址');
            });
        });
    }

    protected function common(Grid $grid){
        $grid->column('order_no');
        $grid->column('pay_way')->using(['余额支付','微信支付'])->label(['primary','info']);
        $grid->column('amount');
        $grid->column('method')->using(['快递配送','到店自提'])->label(['primary','info']);
        $grid->column('address.name','收件人名称');
        $grid->column('address.phone','收件人电话');
        $grid->column('address.address','收件地址');
        $grid->column('order_time')->sortable();
        $grid->column('pay_time')->sortable();
        $grid->column('deliver')->using(['待安排','平台发货','门店发货'])->label(['primary','info','success']);
        $grid->column('shop.name','发货方')->display(function ($value){
            $state = $this->state;
            $is_apply = $this->is_apply;
            return $state>0&&$state<5&&$is_apply==0?($value?:"平台"):"-";
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function order()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->where("state",1);
            $grid->wrap(function (Renderable $view) {
                $tab = Tab::make();
                $admin = Admin::user();
                $isShop = $admin->isAdministrator();
                if ($isShop){
                    $shop_id = $admin->id;
                }else{
                    $shop_id = $admin->shop_id;
                }
                $count = \App\Models\GoodOrder::query()->where("shop_id",$shop_id)->where("is_apply",0)->where("state",1)->count();
                $tab->add("待发货订单({$count})", $view, true);
                $count1 = \App\Models\GoodOrder::query()->where("shop_id",$shop_id)->where("is_apply",0)->where("state",2)->count();
                $tab->add("待收货订单({$count1})", $this->wait1());
                $count2 = \App\Models\GoodOrder::query()->where("shop_id",$shop_id)->where("is_apply",0)->whereIn("state",[3,4])->count();
                $tab->add("已完成订单({$count2})", $this->finish());
                $count3 = \App\Models\GoodOrder::query()->where("shop_id",$shop_id)->where("is_apply",1)->count();
                $tab->add("待退款订单({$count3})", $this->waitRefund());
                $count4 = \App\Models\GoodOrder::query()->where("shop_id",$shop_id)->where("state",5)->count();
                $tab->add("已退款订单({$count4})", $this->refund());
                return $tab;
            });
            $grid->column('state0','发货操作')->display(function (){
                return "发货";
            })->modal("安排发货",function ($model){
                $model->xl();
                return HandleGoodOrder::make()->payload(['oid'=>$this->id]);
            });

        });
    }


    /**
     * 代收货
     *
     * @return Grid
     */
    protected function wait1()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->where("state",2);
            $grid->column('state1','确认收货')->display(function (){
                return "确认收货";
            })->modal("确认收货",function ($model){
                $model->xl();
                return HandleConfirmOrder::make()->payload(['oid'=>$this->id]);
            });
        });
    }

    /**
     * 已完成
     *
     * @return Grid
     */
    protected function finish()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->whereIn("state",[3,4]);
        });
    }

    /**
     * 待退款
     *
     * @return Grid
     */
    protected function waitRefund()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",1);
//            $grid->column('state3','审核操作')->display(function (){
//                return "审核退款";
//            })->modal("审核退款",function ($model){
//                $model->xl();
//                return HandleRefund::make()->payload(['oid'=>$this->id]);
//            });
        });
    }

    /**
     * 待退款
     *
     * @return Grid
     */
    protected function refund()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("state",5);
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodOrder(), function (Form $form) {
            $form->column(12,function (Form $form){
                $model = $form->model();
                $form->html($this->gridDetail($model->order_no),"订单详情");
            });
            $form->column(6,function (Form $form){
                $form->display('order_no');
                $form->display('pay_way')->with(function ($v){
                    $types = ['余额支付','微信支付'];
                    return $types[$v];
                });
                $form->display('amount');
                $form->display('method')->with(function ($v){
                    $types = ['快递配送','到店自提'];
                    return $types[$v];
                });
                $form->display('deliver')->with(function ($v){
                    $types = ['待安排','平台发货','门店发货'];
                    return $types[$v];
                });
                $form->display('state','订单状态')->with(function ($v){
                    $types = \App\Models\GoodOrder::getStateList();
                    return $types[$v];
                });
            });
            $form->column(6,function (Form $form){
                $form->display('address.name','收件人名称');
                $form->display('address.phone','收件人电话');
                $form->display('address.address','收件地址');
                $form->display('order_time');
                $form->display('pay_time');
                $form->display('send_time');
                $form->display('finish_time','完成时间');
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function gridDetail($oid)
    {
        return Grid::make(new GoodOrderDetail(), function (Grid $grid) use ($oid) {
            $grid->disableCreateButton();
            $grid->disableColumnSelector();
            $grid->disableActions();
            $grid->disableRefreshButton();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->column('id')->sortable();
            $grid->model()->where("oid",$oid);
            $grid->column('good_id','商品ID');
            $grid->column('good_name','商品名称');
            $grid->column('good_image','商品图片')->image(null,40,40);
            $grid->column('good_number','商品数量');
            $grid->column('good_price','商品单价');
            $grid->column('total_price','小计');
        });
    }
}
